package com.cloudcentral.controller;

import com.cloudcentral.entity.Auth;
import com.cloudcentral.exception.InvalidException;
import com.cloudcentral.service.AuthService;
import com.cloudcentral.util.Result;
import com.cloudcentral.util.WebUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统菜单权限表 前端控制器
 * </p>
 *
 * @author Administrator
 * @since 2023-10-27
 */
@Slf4j
@RestController
@RequestMapping("/auth")
@Tag(name = "权限管理")
public class AuthController extends BaseController {
  private final AuthService authService;

  @Autowired
  public AuthController(AuthService authService) {
    this.authService = authService;
  }

  @PostMapping("/submit")
  @Operation(summary = "提交")
  public Result<Auth> submit(Auth auth) throws Exception {
    return Result.ok(authService.submit(auth));
  }

  @DeleteMapping("/delete")
  @Operation(summary = "删除")
  public Result<Boolean> delete(Long id) {
    return Result.ok(authService.delete(id));
  }

  @GetMapping("/select")
  @Operation(summary = "查询")
  public Result<Auth> select(Long id) {
    return Result.ok(authService.getById(id));
  }

  @GetMapping("/list")
  @Operation(summary = "列表")
  public Result<List<Auth>> list(@RequestParam Long platformId, @RequestParam(required = false) Long menuId) throws InvalidException {
    return Result.ok(authService.listByUser(platformId, menuId, WebUtils.currentUser().getId()));
  }
}
